import json
import os
import logging

# 创建日志记录器
def create_logger():
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s | 文件:%(filename)s,函数:%(funcName)s')
    return logging.getLogger(__name__)

# 加载语言数据
def load_language_data(logger, file_path='language.json'):
    if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                language_data = json.load(f)
            logger.info(f"成功加载语言数据: {file_path}")
            return language_data
        except json.JSONDecodeError:
            logger.error(f"JSONDecodeError: 无法解析文件 {file_path}")
        except Exception as e:
            logger.error(f"加载文件 {file_path} 时发生未知错误: {e}")
    else:
        logger.warning(f"文件 {file_path} 不存在或者为空")
    return {}

# 写入语言数据
def write_language_data(logger, language_data, key, value, file_path='language.json'):
    language_data[key] = value
    try:
        with open(file_path, 'w', encoding='utf-8') as f:
            json.dump(language_data, f, ensure_ascii=False, indent=4)
        logger.info(f"成功将键 {key} 和值 {value} 写入语言数据文件 {file_path}")
    except Exception as e:
        logger.error(f"写入语言数据文件 {file_path} 时发生错误: {e}")

# 创建日志记录器实例
logger = create_logger()
# 定义全局的 language_data 变量并初始化
language_data = load_language_data(logger)